package com.scau.clothes.api.stock;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scau.clothes.core.core.entity.AjaxResult;
import com.scau.clothes.stock.model.entity.Category;
import com.scau.clothes.stock.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/category")
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @PostMapping("/listCategory")
    @ApiOperation("查询类别")
    public AjaxResult listCategory(@RequestBody Category category){
        List<Category> list =categoryService.listCategory(category);
        return  AjaxResult.success("查询成功",list);
    }
    @PostMapping("/insertCategory")
    @ApiOperation("新增类别")
    public AjaxResult insertCategory(@RequestBody Category category){
        int rows = categoryService.insertCategory(category);
        return rows > 0 ? AjaxResult.success("添加类别成功") : AjaxResult.error("添加类别失败");
    }

    @PostMapping("/updateCategoryInfo")
    @ApiOperation("更新类别信息")
    public AjaxResult updateCategoryInfo(@RequestBody Category category){
        int rows = categoryService.updateCategory(category);
        return rows > 0 ? AjaxResult.success("更新类别信息成功") : AjaxResult.error("更新类别信息失败");
    }

    @PostMapping("/deleteCategory")
    @ApiOperation("删除类别")
    public AjaxResult deleteCategory(@RequestBody Category category){
        int rows = categoryService.deleteCategory(category);
        return rows > 0 ? AjaxResult.success("删除类别成功") : AjaxResult.error("删除类别失败");
    }

    @GetMapping("/page")
    @ApiOperation("分页操作")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Category category){
        List<Category> list=categoryService.listCategory(category);
        Page page =new Page(pageNum,pageSize);
        int total=list.size();
        page.setTotal(total);
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize,total);
        PageHelper.startPage(pageNum,pageSize);
        page.addAll(list.subList(startIndex,endIndex));
        PageInfo pageInfo = new PageInfo<>(page);
        return AjaxResult.success(pageInfo);
    }
}
